/*
 * Copyright (C) 2014 VenomVendor <info@VenomVendor.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.naizhuai.ui.adapter.business;

import android.app.Activity;
import android.content.Context;
import android.media.Image;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.naizhuai.Model.db.user.PersonUserBean;
import com.naizhuai.R;
import com.naizhuai.ui.components.CircleImageView;
import com.naizhuai.ui.entity.mainbusiness.TargetBean;
import com.naizhuai.util.DeviceUtils;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.ListUtils;
import com.naizhuai.util.MathUtils;
import com.naizhuai.util.NaizhuAILog;
import com.naizhuai.util.TimeUtils;
import com.naizhuai.util.ToastCompat;

import java.util.ArrayList;
import java.util.List;

public class TargetMainAdapterHolder extends RecyclerView.Adapter {

    public static final int LIST_TYPE_COMPANY_TARGET = 0;
    public static final int LIST_TYPE_DEPARTMENT_TARGET = 1;
    public static final int LIST_TYPE_PERSONAL_TARGET = 2;

    private ArrayList<Integer> titleIndex;

    private String[] times=null;


    private List<String> operateLists;
    private List<TargetBean> companyTargets;
    private List<TargetBean> departmentTargets;
    private OnItemClickListener mItemClickListener;
    private Activity mContext;
    private String teamInputName;

    public TargetMainAdapterHolder(Activity context, List<String> operateLists, List<TargetBean> companyTargets, List<TargetBean> departmentTargets) {
        this.mContext = context;
        this.operateLists = operateLists;
        this.companyTargets = companyTargets;
        this.departmentTargets = departmentTargets;
        titleIndex = new ArrayList<>();
        titleIndex.add(0);
        titleIndex.add(companyTargets.size() + 1);
        titleIndex.add(companyTargets.size() + departmentTargets.size() + 2);
    }

    public void setTimes(String[] times){
        this.times=times;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_target_item_title_layout, parent, false);
            return new ItemTittleHolder(sView);
        } else {
            View sView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_target_item_layout, parent, false);
            return new TargetItemHolder(sView);
        }
    }

    /**
     * 设置不同的类型：
     * 0：表示展示的title的item
     * 1：展示的item内容
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        initTitleIndex();
        if (titleIndex.contains(position))
            return 0;
        return 1;
    }

    private void initTitleIndex() {
        titleIndex.clear();
        titleIndex.add(0);
        titleIndex.add(companyTargets.size() + 1);
        titleIndex.add(companyTargets.size() + departmentTargets.size() + 2);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemTittleHolder) {
            ItemTittleHolder inputHolder = (ItemTittleHolder) holder;
            if (position < companyTargets.size() + 1)
                position = 0;
            else if (position < companyTargets.size() + departmentTargets.size() + 2)
                position = 1;
            else
                position = 2;
            inputHolder.title.setText(operateLists.get(position));
            if (position == 0&&times!=null) {
                int screenWidth = DeviceUtils.getScreenDevice(mContext)[0];
                double startTime = Double.valueOf(times[0]);
                double endTime = Double.valueOf(times[1]);
                long current = System.currentTimeMillis()/1000;
                //时间完成的比例
                double scoreTime = (current - startTime) / (endTime - startTime);
                inputHolder.progressBar.getLayoutParams().width = (int) (screenWidth * scoreTime);
            }

        } else if (holder instanceof TargetItemHolder) {
            TargetItemHolder manageHolder = (TargetItemHolder) holder;
            List<TargetBean> targetBeans;
            if (position < companyTargets.size() + 1) {
                position = position - 1;
                targetBeans = companyTargets;
            } else {
                position = position - companyTargets.size() - 2;
                targetBeans = departmentTargets;
            }
//            else {
//                position = position - companyTargets.size() - departmentTargets.size() - 3;
//                targetBeans = personalTargets;
//            }
            ImageUtils.glideThumbViewByUrl(mContext, manageHolder.userThumb, targetBeans.get(position).getResponseUserThumb());
            String missionNumber = String.format(mContext.getText(R.string.mission_number).toString(), paramsValue(targetBeans.get(position).getMissionNumber()));
            String joinUserFormat = String.format(mContext.getText(R.string.joined_number_format_extra).toString(), paramsValue(targetBeans.get(position).getJoinedCount()));
            String actionNumberFormat = String.format(mContext.getText(R.string.change_action_format_extra).toString(), paramsValue(targetBeans.get(position).getQuestionCount()));
            manageHolder.targetTitle.setText(targetBeans.get(position).getTargetName());
            manageHolder.missionNumber.setText(missionNumber);
            manageHolder.joinUser.setText(joinUserFormat);
            manageHolder.actionNumber.setText(actionNumberFormat);
        }
    }

    private String paramsValue(String str) {
        return TextUtils.isEmpty(str) ? "0" : str;
    }


    @Override
    public int getItemCount() {
        return operateLists.size() + companyTargets.size() + departmentTargets.size();
    }

    public class ItemTittleHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView title;
        ImageView addImage;
        View progressBar;

        public ItemTittleHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.target_item_title);
            addImage = (ImageView) view.findViewById(R.id.image_add_button);
            progressBar = (View) view.findViewById(R.id.lineProcess);
            addImage.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int position = getAdapterPosition();
            int type = -1;
            if (position == 0)
                type = LIST_TYPE_COMPANY_TARGET;
            else if (position == companyTargets.size() + 1)
                type = LIST_TYPE_DEPARTMENT_TARGET;
            else if (position == companyTargets.size() + departmentTargets.size() + 2)
                type = LIST_TYPE_PERSONAL_TARGET;
            mItemClickListener.onAddClick(v, type);
        }
    }


    public class TargetItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        CircleImageView userThumb;
        TextView targetTitle, missionNumber, joinUser, actionNumber;

        public TargetItemHolder(View view) {
            super(view);
            userThumb = (CircleImageView) view.findViewById(R.id.user_thumb);
            targetTitle = (TextView) view.findViewById(R.id.target_name);
            missionNumber = (TextView) view.findViewById(R.id.mission_number);
            joinUser = (TextView) view.findViewById(R.id.join_user);
            actionNumber = (TextView) view.findViewById(R.id.action_number);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            int index = getAdapterPosition();
            if (mItemClickListener != null) {
                if (index < companyTargets.size() + 1)
                    mItemClickListener.onItemClick(v, index - 1, LIST_TYPE_COMPANY_TARGET);
                else if (index < companyTargets.size() + departmentTargets.size() + 2)
                    mItemClickListener.onItemClick(v, index - companyTargets.size() - 2, LIST_TYPE_DEPARTMENT_TARGET);
                else
                    mItemClickListener.onItemClick(v, index - companyTargets.size() - departmentTargets.size() - 3, LIST_TYPE_PERSONAL_TARGET);
            }
        }
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position, int listIndex);

        void onAddClick(View view, int listAddType);
    }

    public void setOnItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
    }
}
